//
//  PickerViewController.m
//  LoanTeam15
//
//  Created by gynjobs on 2017/8/18.
//  Copyright © 2017年 cangming. All rights reserved.
//

#import "PickerViewController.h"

@interface PickerViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

@end

@implementation PickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGesturePressed:)];
    [self.view addGestureRecognizer:tapGesture];
    [self initData];
    [self initView];
    
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark -
#pragma mark pickerview datasource

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    return [self.dataArray count];
}


#pragma mark -
#pragma mark picker delegate

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    
    NSDictionary * dic = [self.dataArray objectAtIndex:row];
    NSString * title = [dic valueForKey:@"name"];
    return title;
    
}



#pragma mark -
#pragma mark init view

-(void)initView{
    
    self.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    UIView * btnMenu = [self genMenuBtn];
    
    self.dataPickerView = [[UIPickerView alloc] init];
    self.dataPickerView.dataSource = self;
    self.dataPickerView.delegate = self;
    self.dataPickerView.backgroundColor = [UIColor colorWithString:UI_COLOR_MAIN_BG_1];
    [self.view addSubview:self.dataPickerView];
    [self.view addSubview:btnMenu];
    
    
    [self.dataPickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.width.equalTo(self.view);
        make.bottom.equalTo(self.view.mas_bottom);
        make.height.mas_equalTo(216);
    }];
    
    
    [btnMenu mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.width.equalTo(self.view);
        make.bottom.equalTo(self.dataPickerView.mas_top);
        make.height.mas_equalTo(44);
        
    }];
    
    
}

-(void) initData{
    
    ItemListData * itemListData = (ItemListData *)self.itemData;
    NSString * sourceDataKey = itemListData.datasource;
    
    
    NSMutableArray * array = [[GetJsonData standardGetInstance] getJsonBaseData:LOAN_JSON_INFO withKey:sourceDataKey];
    
    if(array){
        
        self.dataArray = array;
        
    }else{
        
        self.dataArray = [[NSMutableArray alloc]initWithCapacity:2];
    }
    
}


-(id)genMenuBtn{
    
    UIView * bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor colorWithString:UI_COLOR_MAIN_BG_1];
    
    
    UIButton * sureBtn = [[UIButton alloc] init];
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    [sureBtn setTitleColor:[UIColor colorWithString:UI_COLOR_MAIN_BG_3] forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(sureBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton * cancelBtn = [[UIButton alloc] init];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:[UIColor colorWithString:UI_COLOR_MAIN_BG_3] forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(cancelBtnPressed:) forControlEvents:UIControlEventTouchUpInside];
    [bgView addSubview:sureBtn];
    [bgView addSubview:cancelBtn];
    
    UIView * lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor colorWithString:UI_COLOR_SPLIT_LINE_1];
    [bgView addSubview:lineView];
    
    
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(bgView.mas_right);
        make.top.equalTo(bgView.mas_top);
        make.height.mas_equalTo(44);
        make.width.equalTo(bgView.mas_width).multipliedBy(0.25);
    }];
    
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(bgView.mas_left);
        make.top.equalTo(bgView.mas_top);
        make.height.mas_equalTo(44);
        make.width.equalTo(bgView.mas_width).multipliedBy(0.25);
    }];
    
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(bgView.mas_left);
        make.right.equalTo(bgView.mas_right);
        make.bottom.equalTo(bgView.mas_bottom);
        make.height.mas_equalTo(1);
        
    }];
    
    return bgView;
    
}

#pragma mark -
#pragma mark tap & btn listener


-(void)tapGesturePressed:(id)sender{
    
    [self dismissSelfPicker];
}

-(void)sureBtnPressed:(id)sender{
    
    
    NSMutableDictionary * dic = [NSMutableDictionary dictionaryWithCapacity:2];
    NSInteger row = [self.dataPickerView selectedRowInComponent:0];
    if ([self.dataArray count] > 0) {
        NSDictionary * loanDic = [self.dataArray objectAtIndex:row];
        NSString * data = [loanDic valueForKey:@"name"];
        id cid = [loanDic valueForKey:@"key"];
        [dic setValue:data forKey:@"data"];
        [dic setValue:cid forKey:@"id"];
        [dic setValue:self.itemData.tip3 forKey:@"score"];
        [self callBackDelegate:dic];
    }
    
    [self dismissSelfPicker];
    
}

-(void)cancelBtnPressed:(id)sender{
    
    [self dismissSelfPicker];
}

-(void)dismissSelfPicker{
    
    [UIView animateWithDuration:0.1 animations:^{
        self.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
    }];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}




@end
